package com.huicoo.forestmanager.ui.patrol.MapFragment.presenter;


import android.text.TextUtils;

import com.huicoo.forestmanager.network.bean.patrol.PatrolToDoList;
import com.huicoo.forestmanager.ui.patrol.MapFragment.contract.MapFragmentContract;
import com.huicoo.forestmanager.ui.patrol.MapFragment.model.MapFragmentModel;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.LogUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MapFragmentPresenter implements MapFragmentContract.Presenter {
    private MapFragmentContract.View view;
    private MapFragmentContract.Model model;

    public MapFragmentPresenter(MapFragmentContract.View view) {
        this.view = view;
        this.model = new MapFragmentModel();
    }

    //	获取待巡林班列表
    @Override
    public void patrolToDoList() {
        Call<PatrolToDoList> call = model.patrolToDoList();
        call.enqueue(new Callback<PatrolToDoList>() {
            @Override
            public void onResponse(Call<PatrolToDoList> call, Response<PatrolToDoList> response) {
                try {
                    PatrolToDoList body = response.body();
                    if (body == null) {
                        //数据请求失败
                        if (view != null) {
                            view.patrolListFail("数据为空");
                        }
                        return;
                    }
                    //数据请求成功
                    LogUtils.e("gogogo 待巡查列表 result = " + JsonUtils.toJson(response.body()));

                    if (response.body() != null && TextUtils.equals(response.body().getCode(), "200")) {
                        if (view != null) {
                            view.patrolListSuccess(response.body());
                        }
                    } else {
                        if (view != null) {
                            view.patrolListFail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "数据为空" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFailure(Call<PatrolToDoList> call, Throwable t) {
                if (view != null) {
                    view.patrolListFail(t.getMessage() + "");
                }
            }
        });

    }


    @Override
    public void destroy() {
        view = null;
    }


}
